<?php

class Homedmin extends BaseAction {

    public function actionList() {
        $page = empty($this->params['page']) ? 1 : $this->params['page'];
        $pagelimit = 10;
        $offset = ($page - 1) * $pagelimit;
       
        $this->setPageTitle('内容管理');
        $articles = ORM_Finder::find('Article', null, '*', 'Created_at DESC', "$offset, $pagelimit");
        
        $count = ORM_Finder::findCount('Article');
        $pager = new Pager(url_for('admin/m/article/list/page/'), $count, $pagelimit);
        $pager->SetCurPage($page);
        
        $this->set('articles', $articles);
        $this->set('pager', $pager);
        $this->render('admin_list');
    }
    
    public function actionEdit() {
        $id = empty($this->params['id']) ? $_POST['id'] : $this->params['id'];
        if (!empty($id)) {
            $article = ORM_Finder::findById('Article', $id);
            $this->setPageTitle('文章修改');
        } else {
            $article = new Article();
            $this->setPageTitle('文章新增');
        }
        if (!empty($_POST)) {
            $title = trim($_POST['title']);
            $content = trim($_POST['content']);
            $description = trim($_POST['description']);
            $author = trim($_POST['author']);
            $link = $_POST['link'];
            $tags = trim($_POST['tags']);
            $old_tags = $article->getTags();
            $state = $_POST['state'];
            $recommend = $_POST['recommend'];
            $date = $_POST['created_at'];
            
            if (!empty($_FILES['pic']['name'])) {
                $pic = $_FILES['pic'];
                $ext = strtolower(substr($pic['name'], strrpos($pic['name'], '.') + 1));
				if ($ext != "jpg" && $ext != 'gif' && $ext != 'png')
				{
					die("illegal file");
				}
                $newfilename = time().'.'.$ext;
                move_uploaded_file($pic['tmp_name'], ROOT.'/files/'.$newfilename);
                $article->setPic($newfilename);
            }
            $article->setTitle($title);
            $article->setContent($content);
            $article->setDescription($description);
            $article->setAuthor($author);
            $article->setLink($link);
            $article->setTags($tags);
            $article->setState($state);
            $article->setRecommend($recommend);
            $article->setClick(0);
            if ($article->isNew() && empty($date)) {
                $date = date('Y-m-d H:i:s', time());
            }
            $article->setCreatedAt($date);
            $article->save();
            
            if ($tags != $old_tags) {
                ArticleTag::deleteWithArticle($article->getId());
                foreach (explode(' ', $tags) as $tag) {
                    $old_tag = ORM_Finder::findOne('Tag', array('name' => $tag));
                    if (empty($old_tag)) {
                        $new_tag = new Tag();
                        $new_tag->setName($tag);
                        $new_tag->setCount(1);
                        $new_tag->setCreated_at(date('Y-m-d H:i:s', time()));
                        $new_tag->save();
                        
                        $article_tag = new ArticleTag();
                        $article_tag->setArticle_id($article->getId());
                        $article_tag->setTag_id($new_tag->getId());
                        $article_tag->setCreatedAt(date('Y-m-d H:i:s'));
                        $article_tag->save();
                    } else {
                        $old_tag->setCount($old_tag->getCount() + 1);
                        $old_tag->save();
                        
                        $new_article_tag = new ArticleTag();
                        $new_article_tag->setArticle_id($article->getId());
                        $new_article_tag->setTag_id($old_tag->getId());
                        $new_article_tag->setCreatedAt(date('Y-m-d H:i:s'));
                        $new_article_tag->save();
                    }
                }
            }
            
            $this->redirect('admin/m/article/list');
        }
        
        $this->set('article', $article);
        $this->render();
    }
    
    public function actionDel() {
        $id = $this->params['id'];
        if (!empty($id)) {
            $article = ORM_Finder::findById('Article', $id);
            if (!empty($article)) {
                ArticleTag::deleteWithArticle($article->getId());
                $article->delete();
            }
        }
        $this->redirect($_SERVER['HTTP_REFERER']);
    }
}
?>